package org.intellij.plugins.junit4;

import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;

/**
 * Factory for JavaFile instances.
 *
 * @author Justin Tomich
 */
public class JavaFileFactory
{
    private final PsiManager psiManager;
    private final ClassNameResolver resolver;


    public JavaFileFactory(PsiManager psiManager, ClassNameResolver resolver)
    {
        this.psiManager = psiManager;
        this.resolver = resolver;
    }


    public JavaFile create(PsiFile file)
    {
        if (file == null) return null;
        
        VirtualFile vf = file.getVirtualFile();
        assert vf != null;
        return create(vf);
    }

    public JavaFile create(VirtualFile file)
    {
        if (file == null) return null;
        
        JavaFileBuilder builder = new JavaFileBuilder();
        builder.setPsiManager(psiManager);
        builder.setResolver(resolver);
        builder.setFile(file);
        return builder.create();
    }

    public JavaFile create(PsiClass psiClass)
    {
        if (psiClass == null) return null;
        
//        if (isInnerClass(psiClass))
//        {
//            throw new RuntimeException("JavaFile not supported for InnerClass!");
//        }

        PsiFile pFile = psiClass.getContainingFile();
        assert pFile != null;
        VirtualFile virtualFile = pFile.getVirtualFile();
        assert pFile != null;
        return create(virtualFile);
    }

//    /**
//     * Get JavaFile for the given element. Does not test to see if element is part of
//     * an inner class.
//     *
//     * @param elem
//     * @return
//     */
//    public JavaFile create(PsiElement elem)
//    {
//        PsiFile pFile = elem.getContainingFile();
//        VirtualFile virtualFile = pFile.getVirtualFile();
//        return create(virtualFile);
//    }


//    boolean isInnerClass(PsiClass psiClass)
//    {
//        final String name = psiClass.getName();
//        if (name == null) return false; // wish i could remember why...
//
//        final PsiFile file = psiClass.getContainingFile();
//        final String fileName = file.getName();
//        return (fileName.indexOf(name) < 0);
//    }
}
